<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\Extend;
use app\admin\model\Group;
use app\admin\model\Member;

/**
 * 权限控制器
 *
 * @author 李忠
 * @createAt 2018-09-06 08:52:55
 *
 */
class Auth extends Admin {

    /**
     * 列表页面
     */
    public function index($title = null, $status = null) {
        cookie('__forward__', $this->request->url());

        // 数据列表
        $data_list = Group::lists($title, $status);
        $this->assign('data_list', $data_list);
        $this->assign('pager', $data_list->render());

        $this->assign('data_status', config('data_status'));
        $this->assign('status', $status);
        $this->assign('title', $title);
        $this->setMeta('权限管理');
        return $this->fetch();
    }

    // 新增权限
    public function add() {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Group');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = Group::create($data)) {
                $this->log($data['id'], '新增用户组成功');
                $this->success('新增用户组成功', cookie('__forward__'));
            } else {
                $this->error('新增用户组失败');
            }
        } else {
            $this->assign('info', null);
            $this->setMeta('新增用户组');
            return $this->fetch();
        }
    }

    // 更新权限
    public function edit($id = null) {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Group');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = Group::update($data)) {

                $this->log($data['id'], '更新用户组成功');
                $this->success('更新用户组成功', cookie('__forward__'));
            } else {
                $this->error('更新用户组失败');
            }

        } else {
            $info = Group::get($id);

            $this->assign('info', $info);
            $this->setMeta('更新用户组');
            return $this->fetch('add');
        }
    }

    public function delete($id) {
        if (Group::state($id, -1)) {
            $this->log($id, '删除用户组成功');
            $this->success('删除用户组成功');
        } else {
            $this->error('删除用户组失败');
        }
    }

    public function resume($id) {
        if (Group::state($id, 1)) {
            $this->log($id, '启用用户组成功');
            $this->success('启用用户组成功');
        } else {
            $this->error('启用用户组失败');
        }
    }

    public function forbid($id) {
        if (Group::state($id, 0)) {
            $this->log($id, '禁用用户组成功');
            $this->success('禁用用户组成功');
        } else {
            $this->error('禁用用户组失败');
        }
    }


    //
    public function user() {
        if ($this->request->isAjax()) {

            $opt = input('o');
            if ($opt == 'd') {
                $user_id = input('u');
                $group_id = input('g');

                $result = Extend::remove($group_id, $user_id, Extend::$TYPE_USER);
                if ($result) {
                    $this->success('解除用户授权成功');
                } else {
                    $this->error('解除用户授权失败'.$result);
                }
            } else if ($opt == 'a') {
                $user_id = input('user_id');

                if (empty($user_id)) {
                    $this->error('用户账号不能为空');
                }
                $user = Member::where(array('status' => 1, 'name' => $user_id))->find();

                if (empty($user)) {
                    $this->error('系统没有或已禁用为' . $user_id . '的用户，请核对后再授权');
                }

                $group_id = input('group_id');

                $count = Extend::where(array('group_id' => $group_id, 'extend_id' => $user['id'], 'type' => Extend::$TYPE_USER))->count();
                if ($count > 0) {
                    $this->error('用户' . $user_id . '已存在该角色授权用户中，不需要重复授权');
                }


                //AuthUserModel::add($group_id, $user_id);
                if (Extend::add($group_id, $user['id'], Extend::$TYPE_USER)) {
                    $this->success('用户授权成功');
                } else {
                    $this->error('用户授权失败');
                }


            }
        } else if ($this->request->isGet()) {
            $id = input('id');
            $info = Group::get($id);
            $this->assign('info', $info);

            //$user_list = AuthUserModel::lists($id);
            $user_list = Extend::listUsers($id);
            $this->assign('user_list', $user_list);
            $this->assign('pager', $user_list->render());
            $this->setMeta('用户授权');
            return $this->fetch();
        }


    }

    public function menu($id) {
        if ($this->request->isPost()) {
            $group_id = input('post.id');
            $data = $this->request->only(['rules']);
            if (empty($data)) {
                $this->error('请选择授权菜单');
            }
            $result = Extend::flush($group_id, $data['rules'], Extend::$TYPE_MENU);
            if ($result) {
                $this->log($group_id, '访问授权成功', null, 'admin', 'Group', 'access');
                $this->success('访问授权成功');
            } else {
                $this->error('访问授权失败');
            }
        } else {

            $info = Group::get($id);

            $menu_list = Extend::listMenus($id, 'admin');

            $this->assign('rule_list', list_to_tree($menu_list->toArray(), 'id', 'pid', '_child', '0'));
            $this->assign('info', $info);

            $this->setMeta('访问授权');
            return $this->fetch();
        }
    }

    public function data($id) {
        if ($this->request->isPost()) {
            $group_id = input('post.id');


            $types = input('post.data_type');
            $data_type = $types[0];

            $result = false;
            if ($data_type != Extend::$TYPE_DATA) {
                $result = Extend::flush($group_id, [$data_type], Extend::$TYPE_DATA);
            } else {
                $data = $this->request->only(['datas']);
                $ids = [];
                foreach ($data['datas'] as $item) {
                    array_push($ids, $item);

                }
                $result = Extend::flush($group_id, $ids, Extend::$TYPE_DATA_CUSTOMER);
            }

            if ($result) {
                $this->log($group_id, '数据授权成功', null, 'admin', 'Group', 'data');
                $this->success('数据授权成功');
            } else {
                $this->error('数据授权失败');
            }
        } else {

            $info = Group::get($id);
            $this->assign('info', $info);

            $this->assign('data_type_list', array('1' => '全部数据范围', '2' => '部门数据范围', '3' => '自定义数据范围'));
            $this->assign('data_type', Extend::getDataType($id));
            $this->assign('data_list', Extend::listDatas($id));
            $this->assign('info', $info);
            $this->setMeta('数据授权');
            return $this->fetch();
        }
    }


}